// components/livestream/index.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    matchinfo: {
      type: Object,
      value: undefined,
      observer: function (newVal, oldVal) {

        this._matchinfopropertyChange(newVal, oldVal);

      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'

    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    islive: false,
    liveurl: '',
    sss: '',
    mmm: '',
    hhh: '',
    
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _matchinfopropertyChange: function (newVal, oldVal) {
     
      var matchinfo = newVal;
      console.log("直播");
      console.log(matchinfo);
      if (matchinfo.matchislivestream == '1') {
        var starttimer = Date.parse(matchinfo.matchislivestreamstarttime);
        let timeDiffe = starttimer - Date.parse(new Date());
        console.log(timeDiffe);
        if (timeDiffe <= 0) { //开始直播
          console.log("开始直播");
          console.log(newVal);
          this.setData({
            islive: true,
            liveurl: matchinfo.matchislivestreamurl,
            timer: 0
          });
        } else { //倒计时
          console.log("开始倒计时");
          console.log(newVal);
          this.count_down(timeDiffe);
        }

      }
    },
    count_down: function (countDown_time) {
      var that = this;
      var time = countDown_time.split(':')
      var hhh = parseInt(time[0])
      var mmm = parseInt(time[1])
      var sss = parseInt(time[2])
      this.setData({
        sss: (sss < 10) ? '0' + sss : sss,
        mmm: (mmm < 10) ? '0' + mmm : mmm,
        hhh: (hhh < 10) ? '0' + hhh : hhh
      })
      var Interval = setInterval(function () {
        if (sss > 0) {
          sss--
        } else {
          console.log('时间到')
          this.setData({
            islive: true,
            liveurl: matchinfo.matchislivestreamurl,
            timer: 0
          });
          clearInterval(Interval)
        }
        if (sss == 0) {
          if (mmm > 0) {
            mmm--
            sss = 59;
          }
          if (mmm == 0 && hhh > 0) {
            hhh--
            sss = 59;
            mmm = 59;
          }
        }
        that.setData({
          sss: (sss < 10) ? '0' + sss : sss,
          mmm: (mmm < 10) ? '0' + mmm : mmm,
          hhh: (hhh < 10) ? '0' + hhh : hhh
        })
      }, 1000)
    },
    statechange(e) {
      console.log('live-player code:', e.detail.code)
    },
    error(e) {
      console.error('live-player error:', e.detail.errMsg)
    }
    
  }
})